05. Exercise: Introduction to Logging

ANDK L4 05b Logging A Callback SC

Now it’s your turn to complete this exercise yourself. Links and instructions for downloading the starter code are in the previous step.

In this exercise you'll add a log statement to the onStart and onCreate callback.

**1. Add a log message to onCreate: **

In MainActivity.kt file locate onCreate callback and add an info level log statement saying onCreate was called.

Below is an example:

Log.i("MainActivity", "onCreate called")

**2. Override the onStart callback method: **

The fastest way to do this is using the Override keyboard shortcut, which is Ctrl + O on all platforms.

**3. Add a log message to onCreate: **

Finish up by adding another log statement to your new onStart method. Make sure to run your code and open up logcat to affirm that both log statements are called when you open the app.

If you want to start at this step, you can download this exercise code from: Step.01-Exercise-Logging-a-callback.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here Step.01-Solution-Logging-a-callback or using this git diff.

Task Description:

Check the steps below as you implement them to complete this exercise.

Task List:

Task Feedback:

Great job!

Solution: Step.01-Solution-Logging-a-callback or using this git diff.

More about Logs

Logs have different levels which are used in different situations. The levels and their usages are listed below:

  • Verbose: Show all log messages (the default).
  • Debug: Show debug log messages that are useful during development only, as well as the message levels lower in this list.
  • Info: Show expected log messages for regular usage, as well as the message levels lower in this list.
  • Warn: Show possible issues that are not yet errors, as well as the message levels lower in this list.
  • Error: Show issues that have caused errors, as well as the message level lower in this list.
  • Assert: Show issues that the developer expects should never happen.

For more information about logging in Android and logcat, check out the documentation.